package com.red.wood.module.ircs.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.ircs.dao.IrcsDomainInfoDao;

@Repository
public class IrcsDomainInfoDaoImpl implements IrcsDomainInfoDao {

	private static final Log log = LogFactory.getLog(IrcsDomainInfoDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(Map<String, Object> param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		list = getList(listPageDataStatistics(param));
		
		count = listCountStatistics(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	/**
	 * 本省企业下的本省用户下的域名
	 * @param param
	 * @return
	 */
	public List<Map<String, Object>> listPageDataStatistics(Map<String, Object> param){
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel, "1")) {//初级权限
			sql = getSqlStatistics(param);
			sql.append(" and t.userProvince = '"+userProvince+"'");
			sql.append(" and t.ircsProvince = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(" limit " + page.getPageSize() + " offset " + offset+ "");
		}else if(StringUtils.equals(userLevel, "2")) {//中级权限
			sql = getSqlStatistics(param);
			sql.append(" and t.ircsProvince = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(" limit " + page.getPageSize() + " offset " + offset+ "");
		}else {//高级权限
			sql.append("select * from (");
			sql.append(getSqlStatistics(param));
			sql.append(" and t.ircsProvince = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(" union all ");
			sql.append(getSqlStatistics(param));
			sql.append(" and t.ircsProvince != '"+userProvince+"'");
			sql.append(" and t.userProvince = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(") a");
			sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		}
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	/**
	 * 本省企业下的本省用户下的域名
	 * @param param
	 * @return
	 */
	public List<Map<String, Object>> listPageData(Map<String, Object> param){
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel, "1")) {//初级权限
			sql = getSql(param);
			sql.append(" and userinfo.province = '"+userProvince+"'");
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(" limit " + page.getPageSize() + " offset " + offset+ "");
		}else if(StringUtils.equals(userLevel, "2")) {//中级权限
			sql = getSql(param);
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(" limit " + page.getPageSize() + " offset " + offset+ "");
		}else {//高级权限
			sql.append("select * from (");
			sql.append(getSql(param));
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(" union all ");
			sql.append(getSql(param));
			sql.append(" and ircs.province != '"+userProvince+"'");
			sql.append(" and userinfo.province = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(") a");
			sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		}
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}

	public StringBuffer getSql(Map<String, Object> param) {
		StringBuffer sql = new StringBuffer();
		sql.append("select domain.domainName,domain.userId,domain.ircsId,ircs.ircsName,userinfo.province,userinfo.unitName as userName,service.regId,service.serviceContent,service.setMode,service.regType");
		sql.append("  from t_ircs_userinfo_serviceinfo_domain domain");
		sql.append(" inner join t_ircs_queryUserInfo userinfo");
		sql.append(" on domain.ircsId = userinfo.ircsId and domain.userId = userinfo.userId");
		sql.append(" inner join t_ircs_ircsdata ircs on domain.ircsId = ircs.ircsId");
		sql.append(" left join t_ircs_userinfo_serviceinfo service ");
		sql.append(" on domain.ircsId = service.ircsId and service.userId = domain.userId and service.serviceId = domain.serviceId");
		sql.append(" where 1 = 1");
		return sql;
	}
	
	public StringBuffer getSqlStatistics(Map<String, Object> param) {
		StringBuffer sql = new StringBuffer();
		sql.append("select t.domainName,t.userId,t.ircsId,t.ircsName,t.userName,t.regId,t.serviceContent,t.setMode,t.regType from t_ircs_domain_statistics t where 1 = 1 ");
		return sql;
	}
	
	public String listCountStatistics(Map<String, Object> param) {
		StringBuffer sql = new StringBuffer();
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		
		if(StringUtils.equals(userLevel, "1")) {//初级权限
			sql.append("select count(*) count from t_ircs_domain_statistics t where 1 = 1");
			sql.append(" and t.userProvince = '"+userProvince+"'");
			sql.append(" and t.ircsProvince = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(" order by t.userName desc");
		}else if(StringUtils.equals(userLevel, "2")) {//中级权限
			sql.append("select count(*) count from t_ircs_domain_statistics t where 1 = 1");
			sql.append(" and t.ircsProvince = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(" order by t.userName desc");
		}else {//高级权限
			sql.append("select count(*) count from (");
			sql.append(getSqlStatistics(param));
			sql.append(" and t.ircsProvince = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(" union all ");
			sql.append(getSqlStatistics(param));
			sql.append(" and t.ircsProvince != '"+userProvince+"'");
			sql.append(" and t.userProvince = '"+userProvince+"'");
			sql.append(getQueryConfStatistics(param));
			sql.append(") a");
		}
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public String listCount(Map<String, Object> param) {
		StringBuffer sql = new StringBuffer();
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		sql.append("select count(*) count from(");
		if(StringUtils.equals(userLevel, "1")) {//初级权限
			sql.append(getSql(param));
			sql.append(" and userinfo.province = '"+userProvince+"'");
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
		}else if(StringUtils.equals(userLevel, "2")) {//中级权限
			sql.append(getSql(param));
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
		}else {//高级权限
			sql.append("select * from (");
			sql.append(getSql(param));
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append(getSql(param));
			sql.append(" and ircs.province != '"+userProvince+"'");
			sql.append(" and userinfo.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}
		sql.append(") t1");
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public List<Map<String,Object>> getList(List<Map<String,Object>> list){
		if(list != null && list.size() > 0) {
			for(Map<String,Object> map : list) {
				String setMode = CommonUtils.valueOf(map.get("setMode"));
				String regType = CommonUtils.valueOf(map.get("regType"));
				if(StringUtils.equals(setMode, "0")) {
					setMode = "专线";
				}else if(StringUtils.equals(setMode, "1")) {
					setMode = "虚拟主机";
				}else if(StringUtils.equals(setMode, "2")) {
					setMode = "虚拟存储";
				}else if(StringUtils.equals(setMode, "3")) {
					setMode = "应用托管容器";
				}else if(StringUtils.equals(setMode, "999")) {
					setMode = "其他";
				}
				if(StringUtils.equals(regType, "0")) {
					regType = "无";
				}else if(StringUtils.equals(regType, "1")) {
					regType = "经营性网站";
				}else if(StringUtils.equals(regType, "2")) {
					regType = "非经营性网站";
				}else if(StringUtils.equals(regType, "3")) {
					regType = "SP";
				}else if(StringUtils.equals(regType, "4")) {
					regType = "BBS";
				}else if(StringUtils.equals(regType, "999")) {
					regType = "其他";
				}
				map.put("setMode", setMode);
				map.put("regType", regType);
			}
		}
		return list;
	}
	
	public StringBuffer getQueryConfStatistics(Map<String,Object> param) {
		String domainName = CommonUtils.valueOf(param.get("domainName"));
		String userName = CommonUtils.valueOf(param.get("userName"));
		String ircsId = CommonUtils.valueOf(param.get("ircsId"));
		String regType = CommonUtils.valueOf(param.get("regType"));
		StringBuffer queryConf = new StringBuffer();
		
		if(StringUtils.isNotBlank(ircsId)) {
			queryConf.append(" and t.ircsId='"+ircsId+"'");
		}
		if(StringUtils.isNotBlank(domainName)) {
			queryConf.append(" and t.domainName like '%"+domainName+"%'");
		}
		if(StringUtils.isNotBlank(userName)) {
			queryConf.append(" and t.userName like '%"+userName+"%'");
		}
		if(!StringUtils.equals(regType, "all")) {
			queryConf.append(" and t.regType = '"+regType+"'");
		}
		return queryConf;
	}
	
	public StringBuffer getQueryConf(Map<String,Object> param) {
		String domainName = CommonUtils.valueOf(param.get("domainName"));
		String userName = CommonUtils.valueOf(param.get("userName"));
		String ircsId = CommonUtils.valueOf(param.get("ircsId"));
		String regType = CommonUtils.valueOf(param.get("regType"));
		StringBuffer queryConf = new StringBuffer();
		
		if(StringUtils.isNotBlank(ircsId)) {
			queryConf.append(" and domain.ircsId='"+ircsId+"'");
		}
		if(StringUtils.isNotBlank(domainName)) {
			queryConf.append(" and domain.domainName like '%"+domainName+"%'");
		}
		if(StringUtils.isNotBlank(userName)) {
			queryConf.append(" and userinfo.unitName like '%"+userName+"%'");
		}
		if(!StringUtils.equals(regType, "all")) {
			queryConf.append(" and service.regType = '"+regType+"'");
		}
		/*if(StringUtils.isNotBlank(userName) || !StringUtils.equals(regType,"all")) {
			if(StringUtils.isBlank(userName) && !StringUtils.equals(regType,"all")) {
				queryConf.append(" having regType='"+regType+"'");
			}else if(StringUtils.isNotBlank(userName) && !StringUtils.equals(regType,"all")) {
				queryConf.append(" having regType='"+regType+"' and userName like '%"+userName+"%'");
			}else if(StringUtils.isNotBlank(userName) && StringUtils.equals(regType,"all")) {
				queryConf.append(" having userName like '%"+userName+"%'");
			}
		}*/
		return queryConf;
	}
}
